home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4297 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  65 lines

  1. Path: svnews.ubinet.ubs.com!ubszh!ubszh!jis
  2. From: jis@ubszh.net.ch (Johnston Ian (by ubsswop))
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help with Bug Pleeeeease!
  5. Date: 29 Jan 1996 16:18:52 GMT
  6. Organization: UBS
  7. Distribution: world
  8. Message-ID: <4eis1c$hbm@ubszh.fh.zh.ubs.com>
  9. References: <tday-2801961838030001@tday.slip.netcom.com>
  10. NNTP-Posting-Host: nol2179.fh.zh.ubs.com
  11.  
  12. In article <tday-2801961838030001@tday.slip.netcom.com>, tday@netcom.com (Tony Day) writes:
  13. |> 
  14. |> I hope some C++ wizard out there can help me debug this apparantly simple
  15. |> program.  I am teaching myself C++ with Prata╣s │C++ primer +▓ and am
  16. |> completely stuck with one of the problems at the end of CH11.
  17. |> 
  18. |> The aim of the excercise is to overload the +operator for a String Class
  19. |> (which consists of a char * and int holding the string length). I have
  20. |> written 3 overloaded + operators, one adds a String to a String (this
  21. |> seems to work), the other two add a C string to a String.  
  22. |> 
  23. |> The problem is with the latter two.  When the assignment operator (not
  24. |> written by me) is invoked after one of these + operators, the whole system
  25. |> crashes (I am using Symantec Think C++ 7.04 on a Mac quadra) even if the
  26. |> assignment operator invokes different Strings to the + operators. This is
  27. |> what happens when the main (below) is run.  If the assignment call is
  28. |> before the +operator call, no problem. 
  29.  
  30. [...]
  31.  
  32. |> String String::operator+(const String & st) const
  33. |> {
  34. |>    String temp1;
  35. |>    delete [] temp1.str;
  36. |>    temp1.len = st.len+len;
  37. |>    temp1.str = new char[temp1.len+1];    <<<<<<<<<<<<<<<<
  38. |>    strcpy(temp1.str,str);
  39. |>    for (int i=len; i< temp1.len; i++)
  40. |>       temp1.str[i]=st.str[i-len];
  41. |>    temp1.str[temp1.len+1]='\0';        <<<<<<<<<<<<<<<<
  42. |>    cout << temp1.len << " length1st\n";
  43. |>    return temp1;
  44. |> }
  45.  
  46. Here's the problem. You are writing over memory you don't own. As you
  47. allocate (temp1.len + 1) bytes, the valid indexes are 0..temp1.len.
  48. You have probably trashed the internal heap management used by new (or
  49. malloc). Interestingly, this didn't crash on a Sun...
  50.  
  51. Wouldn't this have been easier:
  52.  
  53.     strcpy(temp1.str, str);
  54.     strcat(temp1.str, st.str);
  55.  
  56. It may or may not be more efficient, but it is unlikely that strcat() has
  57. any bugs in it...
  58.  
  59. I understand this is an exercise, but you could probably make these
  60. operations more efficient. Mail me if you are interested in further details.
  61.  
  62. Ian
  63.  
  64.  
  65.